<template>
	<view>
		<cu-custom bgColor="linear-gradient(to right, rgba(0,0,0,0.7), rgba(0,0,0,0.6))" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">用户信息</block>
		</cu-custom>
		<view class="cu-form-group">
			<view class="title">是否编辑</view>
			<switch class='switch-sex' @change="SwitchC" :class="switchC?'checked':''" :checked="switchC?true:false"></switch>
		</view>
		<form @submit="formSubmit" @reset="formReset">
			<view class="cu-form-group margin-top">
				<view class="title">用户名</view>
				<input placeholder="请输入用户名" v-model="userinfo.userName" :disabled="!switchC"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">手机号</view>
				<input placeholder="请输入用户名" v-model="userinfo.telPhone" :disabled="!switchC"></input>
			</view>
			<view class="cu-form-group ">
				<view class="title">简介</view>
				<input placeholder="两字短标题" v-model="userinfo.introduction" :disabled="!switchC"></input>
			</view>
			<view class="cu-form-group margin-top" hidden="hidden">
				<view class="title">用户id</view>
				<input placeholder="请输入用户名" v-model="userinfo.userId" :disabled="!switchC"></input>
			</view>
<!-- 			<view class="cu-form-group margin-top">
				<view class="title">头像</view>
				<view class="cu-avatar radius bg-gray"><image class="logo-img" src="http://192.168.3.30:81/images/00001/avatar/1567216095295366.jpg" mode="widthFix"></image></view>
			</view> -->
			
			<view class="uni-btn-v" style="text-align: center;">
				<button class="cu-btn round bg-purple" form-type="submit" :disabled="!switchC">Submit</button>
				<button class="cu-btn round bg-purple" type="default" form-type="reset" :disabled="!switchC">Reset</button>
			</view>
		</form>
	</view>
</template>

<script>
	var _this;
	import {
		updateUser,
	} from '../../../service/api/user.js' //登录api
	export default {
		data() {
			return {
				userinfo:{},
				switchC: false,
			};
		},
		getUserInfo(){
			const value = uni.getStorageSync('setUserData')
			console.log(value)
			userinfo = value
		},
		onLoad(){
			const value = uni.getStorageSync('userinfo');
			console.log(value);
			this.userinfo = value;
			_this = this
		},
		methods: {
			SwitchC(e) {
				this.switchC = e.detail.value
			},
			getUserInfo(){
				const value = uni.getStorageSync('setUserData')
				console.log(value)
				userinfo = value
			},
			formSubmit: function(e) {
			                console.log('form发生了submit事件，携带数据为：' + JSON.stringify(this.userinfo))
			                var formdata = this.userinfo
			                updateUser(formdata)
			                .then(res => {
			                	console.log(res) 
			                	//简单验证下登录（不安全）
			                	if(res.data.code==0){
			                		let userdata={
			                			"username":res.data.data.username,
			                			"userid":res.data.data.userId,
			                			"accesstoken":res.data.accesstoken,
			                		} //保存用户信息和accesstoken
			                		try {
			                			uni.setStorageSync('setUserData', userdata); //存入缓存
										uni.setStorageSync('userinfo', res.data.data); //存入缓存										
			                		} catch (e) {
			                			// error
			                		}
			                		_this.$store.dispatch("setUserData",userdata); //存入状态
			                
			                		_this.isRotate=false
			                		uni.showToast({
			                			icon: 'success',
			                			position: 'bottom',
			                			title: '用户信息修改成功'
			                		});
			                		uni.reLaunch({
			                			url: '../../index?PageCur=mine',
			                		});
			                	}else{
			                		_this.isRotate=false
			                		_this.passData=""
			                		uni.showToast({
			                			icon: 'none',
			                			position: 'bottom',
			                			title: '账号或密码错误'
			                		});
			                	}
			                	uni.hideLoading();
			                }).catch(err => {
			                	console.log(err)
			                	let msg = err.errMsg; //错误信息
			                	uni.showToast({
			                	    icon: 'none',
			                		position: 'center',
			                	    title: '网络开小差了，小主稍后再试哦^.^'
			                	});
			                	_this.isRotate=false
			                	uni.hideLoading();
							})
			},
			formReset: function(e) {
				console.log('清空数据')
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
